@using FOPS.Abstract.MetaInfo.Server
@using FOPS.Abstract.Builder.Enum
@using FOPS.Abstract.Builder.Server
@using FOPS.Abstract.MetaInfo.Entity
@using FOPS.Abstract.Builder.Entity
@using System.Timers
@using FOPS.Abstract.Fss.Entity
@using FOPS.Abstract.Fss.Enum
@using FOPS.Abstract.Fss.Server
@using FS.Extends

@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;
@inject Blazored.LocalStorage.ILocalStorageService storage;
@implements IDisposable;

<div class="layui-table-header">
    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
        <thead>
        <tr>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-task-1">
                    <span>任务名称</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-task-2">
                    <span>计划时间</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-task-3">
                    <span>进度</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-task-4">
                    <span>状态</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-task-5">
                    <span>客户端ID</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-task-6">
                    <span>创建时间</span>
                </div>
            </th>
            <th data-field="Type">
                <div class="layui-table-cell laytable-cell-task-7">
                    <span>操作</span>
                </div>
            </th>
        </tr>
        </thead>
    </table>
</div>
<div class="layui-table-body layui-table-main">
    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
        <tbody>
        @for (var index = 0; index < _lst.Count; index++)
        {
            var info = _lst[index];
            <tr data-index="@index">
                <td>
                    <div class="laytable-cell-task-1"><span lay-tips="@info.JobName">@info.Caption</span></div>
                </td>
                <td>
                    <div class="layui-table-cell laytable-cell-task-2">
                        @{
                            if (info.Status == EumTaskType.Working)
                            {
                                <strong class='layui-badge layui-bg-green'>@($"用时 {(DateTime.Now - info.RunAt).GetDateDesc()}")</strong>
                            }
                            else if ((DateTime.Now - info.StartAt).TotalSeconds > 1)
                            {
                                <strong class='layui-badge'>@($"超时 {(DateTime.Now - info.StartAt).GetDateDesc()}")</strong>
                            }
                            else
                            {
                                @($"等待 {(info.StartAt - DateTime.Now).GetDateDesc()}")
                            }
                        }
                    </div>
                </td>
                <td>
                    <div class="layui-table-cell laytable-cell-task-3">@info.Progress%</div>
                </td>
                <td>
                    <div class="layui-table-cell laytable-cell-task-4">
                        @switch (info.Status)
                        {
                            case EumTaskType.None:
                                <button class="layui-btn layui-btn-xs layui-btn-primary">@info.Status.GetName()</button>
                                break;
                            case EumTaskType.Scheduler:
                                <button class="layui-btn layui-btn-xs layui-btn-normal">@info.Status.GetName()</button>
                                break;
                            case EumTaskType.Working:
                                <button class="layui-btn layui-btn-xs">@info.Status.GetName()</button>
                                break;
                            case EumTaskType.Fail:
                                <button class="layui-btn layui-btn-xs layui-btn-danger">@info.Status.GetName()</button>
                                break;
                            case EumTaskType.Success:
                                <button class="layui-btn layui-btn-xs">@info.Status.GetName()</button>
                                break;
                            case EumTaskType.ReScheduler:
                                <button class="layui-btn layui-btn-xs layui-btn-danger">@info.Status.GetName()</button>
                                break;
                        }
                    </div>
                </td>
                <td>
                    <div class="layui-table-cell laytable-cell-task-5">@info.ClientId</div>
                </td>
                <td>
                    <div class="layui-table-cell laytable-cell-task-6">@info.CreateAt.ToString("yyyy-MM-dd HH:mm:ss")</div>
                </td>
                <td data-field="Title">
                    <div class="layui-table-cell laytable-cell-task-7">
                        @if (info.Status is EumTaskType.Scheduler or EumTaskType.Working)
                        {
                            <button class="layui-btn layui-btn-radius layui-btn-xs" @onclick="() => Cancel(info)">
                                <i class="layui-icon"></i>
                            </button>
                        }
                    </div>
                </td>
            </tr>
        }
        </tbody>
    </table>
</div>
<style>
.laytable-cell-task-1 {width: 200px;}
.laytable-cell-task-2 {width: 150px;}
.laytable-cell-task-3 {width: 60px;}
.laytable-cell-task-4 {width: 80px;}
.laytable-cell-task-5 {width: 160px;}
.laytable-cell-task-6 {width: 150px;}
.laytable-cell-task-7 {width: 80px;}
</style>

@code{
    [Parameter]
    public int PageSize { get; set; } = 20;
    private List<TaskVO> _lst = new();
    private Timer _timer;
    private bool _isFirst;

    protected override async Task OnInitializedAsync()
    {
        if (_isFirst) return;
        _isFirst = true;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await GetList();
            await InvokeAsync(StateHasChanged);

            _timer = new Timer(1000);
            _timer.Elapsed += async (_, _) =>
            {
                try
                {
                    await GetList();
                    await InvokeAsync(StateHasChanged);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            };
            _timer.Enabled = true;
        }
    }
    private async Task GetList()
    {
        _lst = await _iocManager.Resolve<IFssApi>().GetTaskUnFinishListAsync(storage, PageSize);
    }

    private async Task Cancel(TaskVO info)
    {
        await _iocManager.Resolve<IFssApi>().CancelTask(storage,info.TaskGroupId);
    }
    
    public void Dispose()
    {
        _timer?.Dispose();
    }
}